Completed
Push — master ( b76d2e...071101 )
by Askupa
02:04
created

Amarkal.settings.fields.search   A

Complexity

Conditions 1
Paths 3

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
nc 3
nop 1
dl 0
loc 22
rs 9.2
c 2
b 0
f 1

2 Functions

Rating   Name   Duplication   Size   Complexity  
B 0 14 5
A 0 1 1
1
Amarkal.settings.fields = {
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
2
    $form: null,
3
    $fields: null,
4
    init: function () {
5
        this.$form = $('#amarkal-settings-form');
6
        this.$fields = this.$form.find('.amarkal-settings-field');
7
    },
8
    hideAll: function () {
9
        this.hide(this.$fields);
10
    },
11
    showAll: function () {
12
        this.show(this.$fields);
13
    },
14
    showBySection: function (slug) {
15
        this.hideAll();
16
        this.show(this.$fields.filter('[data-section="'+slug+'"]'));
17
    },
18
    search: function(query) {
19
        var matches  = [];
20
        var $form    = this.$form;
21
        
22
        this.$fields.each(function(){
23
            var $field = $(this),
24
                title  = $field.find('h3').text().toLowerCase(),
25
                help = $field.find('.help-content').text();
26
                description = $field.find('.description').text();
0 ignored issues
show
Bug introduced by
The variable description seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.description.
Loading history...
27
28
            // Don't add fields that are currently hidden
29
            if(!$form.amarkalUIForm('isVisible', $field.attr('data-name'))) return;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
30
31
            // Check query against the field's title
32
            if(title.match(query) || description.match(query) || help.match(query)) {
33
                matches.push($field);
34
            }
35
        });
36
37
        // Convert the matches array to a jQuery object
38
        return $(matches).map(function(){return this.toArray();});
39
    },
40
    show: function($fields) {
41
        var _this = this;
42
        $fields.each(function(){
43
            $comp = $(this).find('.amarkal-ui-component');
0 ignored issues
show
Bug introduced by
The variable $comp seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.$comp.
Loading history...
44
45
            // Only show components whose visibility condition is satisfied
46
            if(_this.$form.amarkalUIForm('isVisible', $comp.amarkalUIComponent('getName'))) {
47
                $(this).addClass('visible');
48
                $comp.amarkalUIComponent('refresh');
49
            }
50
        });
51
    },
52
    hide: function($fields) {
53
        $fields.removeClass('visible');
54
    },
55
    flag: function(type, fieldName) {
56
        this.$fields.filter('[data-name="'+fieldName+'"]').addClass('flag-'+type);
57
    },
58
    unflagAll: function() {
59
        this.$fields.removeClass('flag-error flag-notice');
60
    }
61
};